package com.hualan.sfzy.service.impl.consult;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.consult.ConsultExpertiseMapper;
import com.hualan.sfzy.mapper.consult.ExpertiseAreasMapper;
import com.hualan.sfzy.model.entity.ConsultEvaluate;
import com.hualan.sfzy.model.entity.ConsultExpertise;
import com.hualan.sfzy.model.entity.ExpertiseAreas;
import com.hualan.sfzy.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExpertiseAreasServiceImpl implements BaseService<ExpertiseAreas> {
    @Autowired
    private ExpertiseAreasMapper mapper;
    @Autowired
    private ConsultExpertiseMapper expertiseMapper;

    @Override
    public int add(ExpertiseAreas entity) {
        return mapper.insert(entity);
    }

    @Override
    public int edit(ExpertiseAreas entity) {
        return mapper.update(entity);
    }

    @Override
    public int remove(Integer id) {

        List<ConsultExpertise> list = expertiseMapper.selectByExpertiseid(id);

        if (list.size() > 0) {
            throw new RuntimeException("该擅长领域下包含存在的咨询师，不能删除");
        }
        return mapper.delete(id);
    }


    // 没有批量删除操作
    @Override
    public int removeBatch(Integer[] ids) {
        return 0;
    }

    @Override
    public List<ExpertiseAreas> find() {
        return mapper.select(null);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, ExpertiseAreas entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<ExpertiseAreas> list = mapper.select(entity);
        PageInfo<ExpertiseAreas> info = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, info.getPages());
        map.put(CommonConstant.TOTAL, info.getTotal());


        return map;
    }

    @Override
    public ExpertiseAreas findById(Integer id) {
        return mapper.selectById(id);
    }
}
